var arr = [4, 1, 6, 5, 3, 2, 8, 7];

function changeMinMax(arr, i, j) {
  let temp = arr[i];
  arr[i] = arr[j];
  arr[j] = temp;

}

function quickSort(arr, i, j) {
  if (i >= j - 1) return
  let base = arr[i];
  let begin = i;
  let end = j;
  do {
    do {
      i++;
    } while (i < j && arr[i] < base);

    do {
      j--;
    } while (i < j && arr[j] > base)

    if (i < j) changeMinMax(arr, i, j);
  } while (i < j);
  let dot = i == j ? j - 1 : j;
  changeMinMax(arr, begin, dot);
  quickSort(arr, begin, dot);
  quickSort(arr, dot + 1, end);
}

quickSort(arr, 0, arr.length)
console.log(arr);